//
//  ViewController.swift
//  表情键盘
//
//  Created by WGP on 16/4/20.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        textView.inputView = emoticonKeyboard
        emoticonKeyboard.textView = textView
        
        textView.becomeFirstResponder()
    }
    
    
    @IBAction func getText(sender: AnyObject) {
        
        var strM = ""
        
//        属性文本会分段保存,普通文本一段,emoji是一段,附件是一段
//        获取属性文本中的每一段
        textView.attributedText.enumerateAttributesInRange(NSRange(location: 0, length: textView.attributedText.length), options: []) { (dict, range, _) -> Void in
            
//            每当获取到一段内容就会调用闭包一次
//            会返回每段的range
//            图片表情dict里面会包含NSAttachment Key,获取图片表情的名称
            if let attachment = dict["NSAttachment"] as? HMTextAttachment
            {
//                附件chs
                strM += attachment.chs!
            }else
            {
//                普通文本和emoji表情,直接截取对应的内容range
                let text = (self.textView.attributedText.string as NSString).substringWithRange(range)
                
//                将截取的字符串拼接起来
                strM += text
            }
            print("dict: \(dict)")
            print("range: \(range)")
            print("---------")
        }
         print("strM: \(strM)")
    }
    
    private lazy var emoticonKeyboard:HMEmoticonKeyboard = HMEmoticonKeyboard()

}

